define(['jquery', 'bootstrap', 'form', 'table'], function ($, undefined, Form, Table) {
    var Controller = {
        index: function () {
            var table = $("#table");
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/auth/rule/index' + location.search,
                    add_url: '/auth/rule/add',
                    edit_url: '/auth/rule/edit',
                    del_url: '/auth/rule/del',
                    multi_url: '/auth/rule/multi',
                    table: 'auth_rule',
                }
            });

            $(table).bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pagination: false,
                escape: false,
                columns: [
                    {checkbox: true,},
                    {field: 'id', title: 'ID',},
                    {field: 'title', title: '名称', align: 'left'},
                    {field: 'name', title: 'url地址'},
                    {field: 'status', title: '状态', formatter: Table.api.formatter.toggle, yes: 1, no: 0},
                    {field: 'is_menu', title: '是否是菜单', formatter: Controller.api.formatter.status},
                    {field: 'icon', title: '图标', formatter: Table.api.formatter.icon},
                    {field: 'route_name', title: '路由名'},
                    {
                        field: 'id',
                        title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                        operate: false,
                        formatter: Controller.api.formatter.subnode
                    },
                    {
                        field: 'operate',
                        title: '操作',
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate
                    }],
            })
            // 为表格绑定事件
            Table.api.bindevent(table);

            //当内容渲染完成后
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                //默认隐藏所有子节点
                $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
                // $(".btn-node-sub.disabled").closest("tr").hide();

                //显示隐藏子节点
                $(".btn-node-sub").off("click").on("click", function (e) {
                    var status = $(this).data("shown") ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
                        $(this).closest("tr").toggle(!status);
                    });
                    $(this).data("shown", !status);
                    return false;
                });
            });
            //批量删除后的回调
            $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                Fast.api.refreshmenu();
            });
            //展开隐藏一级
            $(document.body).on("click", ".btn-toggle", function (e) {
                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                var that = this;
                var show = $("i", that).hasClass("fa-chevron-down");
                $("i", that).toggleClass("fa-chevron-down", !show);
                $("i", that).toggleClass("fa-chevron-up", show);
                $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
                $(".btn-node-sub[data-pid=0]").data("shown", show);
            });
            //展开隐藏全部
            $(document.body).on("click", ".btn-toggle-all", function (e) {
                var that = this;
                var show = $("i", that).hasClass("fa-plus");
                $("i", that).toggleClass("fa-plus", !show);
                $("i", that).toggleClass("fa-minus", show);
                $(".btn-node-sub.disabled").closest("tr").toggle(show);
                $(".btn-node-sub").data("shown", show);
            });
        },
        add: function () {
            Controller.api.bindevent();
            Controller.events.icon_search();
            Controller.events.route_saerch();
        },
        edit: function () {
            Controller.api.bindevent();
            Controller.events.icon_search();
            Controller.events.route_saerch();
        },
        recyclebin: function () {
            var table = $("#table");
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    recyclebin_url: '/auth/rule/recycleBin',
                }
            });
            $(table).bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
                columns: [
                    {checkbox: true,},
                    {field: 'id', title: 'ID',},
                    {field: 'user_name', title: '用户名',},
                    {field: 'delete_time', title: '删除时间'},
                    {
                        field: 'operate',
                        title: '操作',
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'Restore',
                                text: '还原',
                                classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                icon: 'fa fa-rotate-left',
                                url: '/auth/rule/restore',
                                confirm: '是否确定还原该项',
                                refresh: true
                            },
                            {
                                name: 'Destroy',
                                text: '彻底删除',
                                classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                icon: 'fa fa-times',
                                url: '/auth/rule/destroy',
                                confirm: '是否确定彻底删除该项',
                                refresh: true
                            }
                        ],
                    }],
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        events: {
            icon_search: function () {
                var iconlist = [];
                var iconfunc = function () {
                    require(['template'], function (template) {
                        layer.open({
                            type: 1,
                            area: ['99%', '98%'], //宽高
                            content: template('chooseicontpl', {iconlist: iconlist})
                        });
                    })
                };
                $(document).on('click', '.btn-icon', function () {
                    if (iconlist.length == 0) {
                        $.get("/static/backend/libs/font-awesome/less/variables.less", function (ret) {
                            var exp = /fa-var-(.*):/ig;
                            var result;
                            while ((result = exp.exec(ret)) != null) {
                                iconlist.push(result[1]);
                            }
                            iconfunc();
                        });
                    } else {
                        iconfunc();
                    }
                })
                $(document).on('click', '#chooseicon ul li', function () {
                    var values = 'fa fa-' + $(this).data("font");
                    $("#icon-show").addClass(values)
                    $("input[name='row[icon]']").val(values);
                    layer.closeAll();
                });
                $(document).on('keyup', 'input.js-icon-search', function () {
                    $("#chooseicon ul li").show();
                    if ($(this).val() != '') {
                        $("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();
                    }
                });

            },
            route_saerch: function () {
                return false;
                var route_list = [];
                var routefunc = function () {
                    require(['template'], function (template) {
                        layer.open({
                            type: 1,
                            area: ['99%', '98%'], //宽高
                            content: template('route_name', {route_list: route_list})
                        });
                    })
                };
                $(document).on('click', '.btn-route_name', function () {
                    if (route_list.length == 0) {
                        $.get("/admin/ajax/routeSearch", function (ret) {

                            var exp = /fa-var-(.*):/ig;
                            var result;
                            while ((result = exp.exec(ret)) != null) {
                                route_list.push(result[1]);
                            }
                            routefunc();
                        });
                    } else {
                        routefunc();
                    }
                })
            },
            route_name: function () {
                $("#c-route_name").on('blur', function () {
                    var value = $(this).val();
                    var rule = new RegExp('/', 'g')
                    var last_value = value.replace(rule, '_');
                    $(this).val(last_value);
                })
            }
        },

        api: {
            bindevent: function () {
                var form = $('form[role="form"]');
                Form.api.bindevent(form);
                Controller.events.route_name();
            },
            formatter: {
                status: function (value, row, index) {
                    var classes = 'label label-dark',
                        text = '否';
                    if (value == 1) {
                        classes = 'label label-success';
                        text = '是';
                    }
                    return '<label class="' + classes + '">' + text + '</label>';
                }, subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="切换子菜单" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                }
            }
        }

    };
    return Controller;
});
